<?php


namespace app\service\user;


use app\model\coach\CoachRatioModel;
use app\model\partner\EscortMenuReserveOrderModel;
use app\model\partner\UserPartnerModel;
use app\service\BaseService;

class UserPartnerService extends BaseService
{

    public function __construct()
    {
        $this->model = new UserPartnerModel();
    }

    /**
     * 计算教练转化率
     * @param $partnerId
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function calculatePartnerTurnRatio($partnerId)
    {
        $orderList = EscortMenuReserveOrderModel::where('coach_user_id', $partnerId)
            ->where('status', 'in', [3, 4, 5, 7, 8])
            ->column('is_trial');
        $arr = array_count_values($orderList);
        $totalExperienceOrderCount = $arr[1] ?? 0;
        $totalOfficialOrderCount = $arr[0] ?? 0;

        $total = CoachRatioModel::where('coach_id', $partnerId)->count();
        $success = CoachRatioModel::where('coach_id', $partnerId)->where('status', 1)->count();
        $totalRatio = $total == 0 ? 0 : intval($success / $total * 100);
        return ['experience_count' => $totalExperienceOrderCount, 'buy_count' => $totalOfficialOrderCount];
    }
}